package com.workingpub.commons.jdbc.builder;

import java.util.HashMap;
import java.util.Map;

import com.workingpub.commons.jdbc.binding.SqlMapping;

/**
 * 封装SQL解析所需要的输入和输出资源
 * 
 * @author taoping
 *
 */
public class BuildContext {

	// 输入资源
	private SqlMapping sqlMapping;
	private Object rootParameter;
	private Class<?> returnType;
	private Integer pageNumber;
	private Integer pageSize;
	private Map<String, Object> otherParameters;

	// 输出资源
	private StringBuilder sourceSql;
	private Map<String, Object> sourceParams;

	public BuildContext(SqlMapping sqlMapping, Object parameter, Class<?> returnType) {
		this(sqlMapping, parameter, returnType, null, null);
	}

	public BuildContext(SqlMapping sqlMapping, Object parameter, Class<?> returnType, Integer pageNumber,
			Integer pageSize) {
		this.sqlMapping = sqlMapping;
		this.rootParameter = parameter;
		this.returnType = returnType;
		this.pageNumber = pageNumber;
		this.pageSize = pageSize;
	}

	public SqlMapping getSqlMapping() {
		return sqlMapping;
	}

	public Object getRootParameter() {
		return rootParameter;
	}

	public Map<String, Object> getOtherParameters() {
		return otherParameters;
	}

	public void putOtherParameter(String key, Object value) {
		if (null == otherParameters) {
			otherParameters = new HashMap<String, Object>();
		}
		otherParameters.put(key, value);
	}

	public Class<?> getReturnType() {
		return returnType;
	}

	public Integer getPageNumber() {
		return pageNumber;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public StringBuilder getSourceSql() {
		return sourceSql;
	}

	public void appendSqlSegment(String sqlSegment) {
		if (null == sourceSql) {
			sourceSql = new StringBuilder();
		}
		sourceSql.append(sqlSegment);
	}

	public Map<String, Object> getSourceParams() {
		return sourceParams;
	}

	public void addSourceParam(String key, Object value) {
		if (null == sourceParams) {
			sourceParams = new HashMap<String, Object>();
		}
		sourceParams.put(key, value);
	}

}
